package net.WMisiedjan.BukkitToDate.Launcher;

import java.io.File;
import java.io.IOException;


public class Launcher {
	public static BukkitLauncher launcher = null;

	public static void main(String[] args) {
		NoExitSecurityManager security = new NoExitSecurityManager();
		
		security.setUp();
		/*
		Runtime.getRuntime().addShutdownHook(new Thread() {
	          @Override
	          public void run() {   
				
				Launcher.launcher.UNLOAD();
				
				File file = new File("craftbukkit.jar");

				if (!file.exists()) {
					System.out.println("Craftbukkit.jar doesn't exists.");
					System.exit(1);
					// TODO: Automatic download craftbukkit.jar
				}
				
				System.out.println("[DEBUG] POINT 1");
				
				Launcher.launcher.BTDHandler = new BTDClass();
				Launcher.launcher.BTDHandler.checkConfig();
				
				System.out.println("[DEBUG] POINT 2");
				if (Launcher.launcher.BTDHandler.btd_switchto != 0) {
					System.out.println("[DEBUG] POINT 3");
					System.out.println("A switch to build "
							+ Launcher.launcher.BTDHandler.btd_switchto
							+ " is requested by plugin!");
					System.out
							.println("Downloading build "
									+ Launcher.launcher.BTDHandler.btd_switchto
									+ "....");

					File buildfile = new File("bukkit_build/craftbukkit_build_"
							+ Launcher.launcher.BTDHandler.btd_switchto + ".jar");

					if (buildfile.exists()) {
						if (Launcher.launcher.BTDHandler
								.SwitchTo(Launcher.launcher.BTDHandler.btd_switchto, Launcher.launcher.BTDHandler.btd_recommendbuilds)) {
							System.out.println("Succesly moved build:"
									+ Launcher.launcher.BTDHandler.btd_switchto);
							Launcher.launcher = new BukkitLauncher();
						} else {
							System.out.println("ERROR MOVING BUILD: "
									+ Launcher.launcher.BTDHandler.btd_switchto);
							return;
						}
					} else {
						try {
							if (Launcher.launcher.BTDHandler
									.DownloadJenkisBuild(Launcher.launcher.BTDHandler.btd_switchto, Launcher.launcher.BTDHandler.btd_recommendbuilds)) {
								System.out.println("Succesly downloaded build:"
										+ Launcher.launcher.BTDHandler.btd_switchto);
								System.out.println("Applying new Craftbukkit.jar");
								if (Launcher.launcher.BTDHandler
										.SwitchTo(Launcher.launcher.BTDHandler.btd_switchto, Launcher.launcher.BTDHandler.btd_recommendbuilds)) {
									System.out
											.println("Succesly moved build:"
													+ Launcher.launcher.BTDHandler.btd_switchto);
									Launcher.launcher = new BukkitLauncher();
								} else {
									System.out
											.println("ERROR MOVING BUILD: "
													+ Launcher.launcher.BTDHandler.btd_switchto);
									return;
								}

							}
						} catch (IOException er) {
							// TODO Auto-generated catch block
							er.printStackTrace();
						}

					}
				} else {
					System.out.println("[DEBUG] POINT 5");
					Launcher.launcher = new BukkitLauncher();
				}
	          }
	         });
		*/
		
		try
		{
			launcher = new BukkitLauncher();
		}
		catch(SecurityException e)
		{
			System.out.println("BINGO!");
			  try {
					System.in.read();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		}
	}
}
